﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace Hazop
{
    public partial class GeneralAdvicePage : UserControl
    {
        public GeneralAdvicePage(){
            InitializeComponent();
            _bindList.DataSource = Program.Workspace.project.GeneralAdvices;
            dgvGeneralAdvice.DataSource = _bindList;
        }

        private void addNewRow_Click(object sender, EventArgs e)
        {
            if (!dgvGeneralAdvice.Enabled)
            {
                return;
            }
            GeneralAdviceItem item = new GeneralAdviceItem();
            _bindList.Insert(dgvGeneralAdvice.Rows.Count, item);
            dgvGeneralAdvice.Rows[dgvGeneralAdvice.Rows.Count - 1].Selected = true;
            dgvGeneralAdvice.CurrentCell = dgvGeneralAdvice[1, dgvGeneralAdvice.Rows.Count - 1];
        }

        private void btnEdit_Click(object sender, EventArgs e){
            dgvGeneralAdvice.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e){
            dgvGeneralAdvice.Enabled = false;
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            MessageBox.Show("通用建议保存成功!");
        }

        private void dgvGeneralAdvice_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvGeneralAdvice.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvGeneralAdvice.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvGeneralAdvice.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dgvGeneralAdvice_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int col = dgvGeneralAdvice.CurrentCell.ColumnIndex;
            int row = dgvGeneralAdvice.CurrentCell.RowIndex;
            if (dgvGeneralAdvice.CurrentCell.OwningColumn.Name == "advice" && dgvGeneralAdvice.CurrentCell.Value != null)
            {
                if(dgvGeneralAdvice.Rows[row].Cells[col - 1].Value == null){
                    dgvGeneralAdvice.Rows[row].Cells[col - 1].Value = "G--" +
                                                                      Program.Workspace.project.GetGeneralAdvice();
                }
            }
        }
    }
}
